//
//  FSAddAttentionController.m
//  fanshu
//
//  Created by 许海峰 on 16/3/29.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSAddAttentionController.h"
#import "FSSearchController.h"
#import "FSSpecialTopicAddAttentionViewController.h"
#import "FSUserAddAttentionViewController.h"
@interface FSAddAttentionController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UISegmentedControl *segment;
@property (nonatomic,weak) UIScrollView *scrollView;
@end

@implementation FSAddAttentionController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    //导航条
    [self setupNavBar];
    //添加scrollView
    [self setupScrollView];
    //子导航条
    [self setupSegmentedControl];
    //添加子控制器
    [self setupAllChildVC];
    
    self.segment.selectedSegmentIndex = 0;
    [self setupChildVCViewAtIndex:0];
    
}
//添加scrollView
-(void)setupScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    NSInteger count = 2;
    scrollView.contentSize = CGSizeMake(count * self.view.width, self.view.height);
    self.scrollView = scrollView;
    scrollView.backgroundColor = [UIColor grayColor];
    
    scrollView.pagingEnabled = YES;
    scrollView.scrollsToTop = NO;
    
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
}
//添加子控制器
-(void)setupAllChildVC
{
    
    FSSpecialTopicAddAttentionViewController *specialTopicVC1 = [[FSSpecialTopicAddAttentionViewController alloc] init];
    [self addChildViewController:specialTopicVC1];
    
    
    //临时测试用cell
    FSUserAddAttentionViewController *specialTopicVC2 = [[FSUserAddAttentionViewController alloc] init];
    [self addChildViewController:specialTopicVC2];
}
//添加设置导航条
-(void)setupNavBar
{
    self.navigationItem.title = @"添加关注";
    UIImage *searchImage = [UIImage imageNamed:@"icon_search"];
    searchImage = [searchImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:searchImage style:UIBarButtonItemStylePlain target:self action:@selector(searchBtnClick)];
}
//后退按钮点击
-(void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)searchBtnClick
{
    //model  搜索控制器
    FSSearchController *Vc = [[FSSearchController alloc] init];
    Vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:Vc animated:YES completion:nil];

}
//设置segment
-(void)setupSegmentedControl
{
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"专题",@"用户"]];
    seg.frame = CGRectMake(0, 64, self.view.width, 35);
    seg.backgroundColor = [UIColor lightGrayColor];
    seg.tintColor = [UIColor lightGrayColor];
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    dict1[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [seg setTitleTextAttributes:dict1 forState:UIControlStateNormal];
    
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    dict2[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [seg setTitleTextAttributes:dict2 forState:UIControlStateSelected];
    [self.view addSubview:seg];
    
    self.segment = seg;
    [seg addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
    
}
//点击了切换按钮
-(void)segmentChange:(UISegmentedControl *)segment
{
//    NSLog(@"%ld",segment.selectedSegmentIndex);
    NSInteger index = segment.selectedSegmentIndex;
    [UIView animateWithDuration:0.25 animations:^{
        self.scrollView.contentOffset = CGPointMake(index * self.scrollView.width, 0);
    }completion:^(BOOL finished) {
        //切换完成加载子控制器的view
        [self setupChildVCViewAtIndex:index];
    }];
    for (int i = 0; i < segment.numberOfSegments; i++) {
        UITableViewController *childVC = self.childViewControllers[i];
        if (![childVC isViewLoaded]) {
            continue;
        }
        if (![childVC isKindOfClass:[UITableViewController class]]) {
            continue;
        }
        childVC.tableView.scrollsToTop = (i == index);
    }
}
//加载子控制器的view
-(void)setupChildVCViewAtIndex:(NSInteger)index
{
    UITableViewController *childVC = self.childViewControllers[index];
    if ([childVC isViewLoaded]) {
        return;
    }

    childVC.view.frame = CGRectMake(self.scrollView.width * index, 0, self.scrollView.width, self.scrollView.height);
    [self.scrollView addSubview:childVC.view];
//    childVC.tableView.scrollsToTop = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    self.segment.selectedSegmentIndex = index;
    [self setupChildVCViewAtIndex:index];
    for (int i = 0; i < self.segment.numberOfSegments; i++) {
        UITableViewController *childVC = self.childViewControllers[i];
        if (![childVC isViewLoaded]) {
            continue;
        }
        if (![childVC isKindOfClass:[UITableViewController class]]) {
            continue;
        }
        childVC.tableView.scrollsToTop = (i == index);
    }

}
@end
